package cn.xny.org.controller;


import cn.xny.basic.exception.BusinessException;
import cn.xny.basic.util.ExcelUtils;
import cn.xny.org.domain.Shop;
import cn.xny.org.domain.ShopAuditLog;
import cn.xny.org.dto.ShopDto;
import cn.xny.org.query.ShopQuery;
import cn.xny.basic.util.AjaxResult;
import cn.xny.basic.util.PageList;
import cn.xny.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadByAll(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query) {

        return shopService.pageQuery(query);
    }

     //校验店铺名字
      @PostMapping("/settlement")
        public AjaxResult settlement(@RequestBody Shop shop){
          try {
              shopService.settlement(shop);
              return new AjaxResult();
          } catch (BusinessException e) {
              e.printStackTrace();
              return new AjaxResult(false,e.getMessage());
          } catch (Exception e) {
              e.printStackTrace();
              return new AjaxResult(false,"网络错误，请稍后");
          }


      }
      //审核通过
      @PostMapping("/audit/pass")
        public AjaxResult auditPass(@RequestBody ShopAuditLog log){
          try {
              shopService.auditPass(log);
              return new AjaxResult();
          } catch (Exception e) {
              e.printStackTrace();
              return new AjaxResult(false,e.getMessage());
          }


      }
      //审核失败
      @PostMapping("/audit/reject")
        public AjaxResult auditReject(@RequestBody ShopAuditLog log){
          try {
              shopService.auditReject(log);
              return new AjaxResult();
          } catch (Exception e) {
              e.printStackTrace();
              return new AjaxResult(false,e.getMessage());
          }


      }
      //文件导出
      @GetMapping("/export")
    public void exportExport(HttpServletResponse response){

        List<Shop> shops = shopService.loadAll();
          //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response
          ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"shop.xlsx",response);
      }
      //文件导入
      @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file){

          List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
          for (Shop shop : shops) {
              System.out.println(shop);
          }
      }

      //店铺激活
    @GetMapping("/active/{shopId}")
    public String auditActive(@PathVariable("shopId") Long shopId) {
            shopService.auditActive(shopId);
            return "<h1>入驻成功</h1>";
    }

    //echarts统计部门状态
    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        return shopDtos;
    }
}
